<script setup>
import {reactive, ref} from 'vue'
import { onLoad } from "@dcloudio/uni-app";
import SelectPopup from "@/components/selectPopup.vue";
import DatePopup from "@/components/datePopup.vue";
import {baseUrl, host} from "@/utils/config";
import {getDict, uploadUrl, wxLogin} from "@/api";
import Uploader from "@/components/uploader.vue";
import {modifyDtl, getDetail} from "@/api/task";
import { getFileName } from "@/utils";

const buildDict = () => {
  getDict().then(res => {
    console.log('dictres', res)
    const data = res.data
    const dict = {
      store_status: [],
      check_status: [],
      store_type: [],
      task_status: []
    }
    for (let i = 0; i < data.length; i++) {
      dict[data[i].dict_type] = [
        ...dict[data[i].dict_type],
        ...[
          {
            code: data[i].dict_code,
            text: data[i].dict_label,
            value: data[i].dict_value,
            type: data[i].list_class
          }
        ]
      ]
    }
    console.log('dict', dict)
    uni.setStorageSync('dict', dict)
  })
}
const login = () => {
  return new Promise((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success: async (res) => {
        console.log('res', res)
        const params = {
          code: res.code
        }
        await wxLogin(params).then(async res => {
          console.log('login', res)
          await uni.setStorageSync('token', res.data.token)
          Object.assign(userInfo, res.data)
          await uni.setStorageSync('userInfo', res.data)
          await buildDict()
          resolve()
        })
      }
    })
  })
}
onLoad(async (e) => {
  console.log('eeeeeeeeee', e)
  if (e?.from === 'wxmsg') {
    await login()
  }
  const params = {
    id: e.id
  }
  getDetail(params).then(res => {
    pageData.formData = res.data
    for (const task of pageData.formData.fileList) {
      for (const file of task.fileinfoList) {
        file.name = getFileName(file.fileUrl)[0]
        file.extname = getFileName(file.fileUrl)[1]
      }
    }
  })
})

const userInfo = uni.getStorageSync('userInfo')
const dict = reactive({})
const formData = ref(null)
const pageData = reactive({
  activeCollapse: 'baseInfo',
  formData: {
    name: null,
    safeResp: null,
    respList: null,
    content: null,
    fileList: []
  },
  taskList: []
})
const formRule = {
  deptName: [
    {
      required: true,
      message: '请选择部门'
    }
  ]
}
Object.assign(dict, uni.getStorageSync('dict'))

const loadingBtn = ref(false)
const handleModify = (task) => {
  console.log('task', task)
  for (const file of task.fileinfoList) {
    file.name = getFileName(file.fileUrl)[0]
    file.extname = getFileName(file.fileUrl)[1]
    file.fileType = 2
  }
  loadingBtn.value = true
  modifyDtl(task).then(res => {
    uni.navigateBack()
    uni.showToast({
      title: '提交成功'
    })
  }).catch(err => {
    loadingBtn.value = false
  })
}
</script>

<template>
  <nut-form :model-value="pageData.formData" ref="formData">
    <nut-collapse v-model="pageData.activeCollapse" :accordion="true">
      <nut-collapse-item title="基础信息" name="baseInfo">
        <nut-form-item label="任务名称" prop="name">
          {{ pageData.formData.name }}
        </nut-form-item>
        <nut-form-item label="安全职责" prop="safeResp">
          {{ pageData.formData.safeResp }}
        </nut-form-item>
        <nut-form-item label="履职清单" prop="respList">
          {{ pageData.formData.respList }}
        </nut-form-item>
        <nut-form-item label="履职内容" prop="content">
          {{ pageData.formData.content }}
        </nut-form-item>
      </nut-collapse-item>
      <nut-collapse-item v-for="(item, index) in pageData.formData.fileList" :key="item.id" :title="item.createTime" :name="item.createTime">
        <template v-slot:title>
          <nut-tag mark plain :type="item.fileinfoList.length ? 'success' : 'danger'" style="margin-left: 20px;">{{ item.fileinfoList.length ? '已上传' : '未上传' }}</nut-tag><view style="display: inline;padding-left: 10px;">{{ item.createTime }}</view>
        </template>
        <nut-form-item label="文件" prop="file">
          <uploader v-model="item.fileinfoList" :url="uploadUrl" url-field="fileUrl" type-field="fileType" file-mediatype="all" :limit="3"></uploader>
        </nut-form-item>
        <view class="btn">
          <nut-button :loading="loadingBtn" block type="primary" @click="handleModify(item)">
            提交
          </nut-button>
        </view>
      </nut-collapse-item>
    </nut-collapse>
  </nut-form>
</template>

<style scoped lang="scss">

</style>
